package com.atguigu.encryption.controller;

import com.atguigu.encryption.param.*;
import com.atguigu.encryption.service.SymmetricService;
import com.atguigu.encryption.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.expression.spel.ast.NullLiteral;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@Slf4j
public class SymmetricController {

    @Autowired
    private SymmetricService symmetricService;


    @PostMapping("symmetric/desEncrypt")
    public R desEncrypt(@RequestBody @Validated EncryptParam encryptParam, BindingResult result) {

        if (encryptParam.getPlaintext() == null || encryptParam.getPlaintext().equals("") || encryptParam.getPassword() == null) {
            return R.fail("参数异常");
        }

        return symmetricService.desEncrypt(encryptParam.getPlaintext(), encryptParam.getPassword());

    }

    @PostMapping("symmetric/desDecrypt")
    public R desDecrypt(@RequestBody @Validated DecryptParam decryptParam) {
        if (decryptParam.getCiphertext() == null || decryptParam.getCiphertext().equals("")) {
            return R.fail("参数异常");
        }

        return symmetricService.desDecrypt(decryptParam.getCiphertext(), decryptParam.getPassword());
    }

    /**
     * AES加密
     * @param encryptParam
     * @param result
     * @return
     */
    @PostMapping("symmetric/aesEncrypt")
    public R aesEncrypt(@RequestBody @Validated EncryptParam encryptParam, BindingResult result) {

        log.info("Received EncryptParam: {}", encryptParam.toString());

        if (encryptParam.getPlaintext() == null || encryptParam.getPlaintext().equals("") || encryptParam.getPassword() == null) {
            return R.fail("参数异常");
        }

        return symmetricService.aesEncrypt(encryptParam.getPlaintext(), encryptParam.getPassword());

    }

    /**
     * AES解密
     * @param decryptParam
     * @return
     */
    @PostMapping("symmetric/aesDecrypt")
    public R aesDecrypt(@RequestBody @Validated DecryptParam decryptParam) {
        if (decryptParam.getCiphertext() == null || decryptParam.getCiphertext().equals("")) {
            return R.fail("参数异常");
        }

        return symmetricService.aesDecrypt(decryptParam.getCiphertext(), decryptParam.getPassword());
    }

    /**
     * SM4加密
     * @param encryptParam
     * @param result
     * @return
     */
    @PostMapping("symmetric/sm4Encrypt")
    public R sm4Encrypt(@RequestBody @Validated EncryptParam encryptParam, BindingResult result) {

        if (encryptParam.getPlaintext() == null || encryptParam.getPlaintext().equals("") || encryptParam.getPassword() == null) {
            return R.fail("参数异常");
        }

        return symmetricService.sm4Encrypt(encryptParam.getPlaintext(), encryptParam.getPassword());

    }

    /**
     * SM4解密
     * @param decryptParam
     * @return
     */
    @PostMapping("symmetric/sm4Decrypt")
    public R sm4Decrypt(@RequestBody @Validated DecryptParam decryptParam) {
        if (decryptParam.getCiphertext() == null || decryptParam.getCiphertext().equals("")) {
            return R.fail("参数异常");
        }

        return symmetricService.sm4Decrypt(decryptParam.getCiphertext(), decryptParam.getPassword());
    }

    @PostMapping("getAllEncryption")
    public R getAllEncryption(@RequestBody PageParam pageParam) {
        return symmetricService.getAllEncryption(pageParam);
    }

    @PostMapping("getAllDecryption")
    public R getAllDecryption(@RequestBody PageParam pageParam) {
        return symmetricService.getAllDecryption(pageParam);
    }

    @PostMapping("irreversible/md5")
    public R md5(@RequestBody @Validated NoPasswordParam noPasswordParam) {
        if (noPasswordParam.getPlaintext() == null || noPasswordParam.getPlaintext().equals("")) {
            return R.fail("参数异常");
        }

        return symmetricService.md5(noPasswordParam.getPlaintext());
    }

    @PostMapping("irreversible/sha1")
    public R sha1(@RequestBody @Validated NoPasswordParam noPasswordParam) {
        if (noPasswordParam.getPlaintext() == null || noPasswordParam.getPlaintext().equals("")) {
            return R.fail("参数异常");
        }
        return symmetricService.sha1(noPasswordParam.getPlaintext());
    }

    /**
     *  sha256
     * @param noPasswordParam
     * @return
     */
    @PostMapping("irreversible/sha256")
    public R sha256(@RequestBody @Validated NoPasswordParam noPasswordParam){
        if(noPasswordParam.getPlaintext() == null || noPasswordParam.getPlaintext().equals("")){
            return R.fail("参数异常");
        }
        return symmetricService.sha256(noPasswordParam.getPlaintext());
    }


    /**
     * 查询密码
     *
     * @param
     * @return
     */
    @PostMapping("/query")
    public R queryCiphertext(@RequestBody @Validated QueryParam queryParam){
        if(queryParam.getPlaintext() == null
                || queryParam.getPlaintext().equals("")
                || queryParam.getMethod() ==null ){
            return R.fail("参数异常");
        }
        return symmetricService.queryCiphertext(queryParam.getPlaintext(),queryParam.getPassword(),queryParam.getMethod());
    }


    @PostMapping("/query/Nopassword")
    public R queryCiphertext1(@RequestBody @Validated QueryParam queryParam){
        if(queryParam.getPlaintext() == null
                || queryParam.getPlaintext().equals("")
                || queryParam.getMethod() ==null ){
            return R.fail("参数异常");
        }
        return symmetricService.queryCiphertext1(queryParam.getPlaintext(),queryParam.getMethod());
    }


    @PostMapping("/query/redis")
    public R queryCiphertextByRedis(@RequestBody @Validated QueryParam queryParam){
        if(queryParam.getPlaintext() == null
                || queryParam.getPlaintext().equals("")
                || queryParam.getMethod() ==null ){
            return R.fail("参数异常");
        }
        return symmetricService.queryCiphertextByRedis(queryParam.getPlaintext(),queryParam.getPassword(),queryParam.getMethod());
    }

    /**
     * SM3,只可以加密，但是不能解密
     * @param noPasswordParam
     * @return
     */
    @PostMapping("irreversible/sm3")
    public R sm3(@RequestBody @Validated NoPasswordParam noPasswordParam){
        if(noPasswordParam.getPlaintext() == null || noPasswordParam.getPlaintext().equals("")){
            return R.fail("参数异常");
        }
        return symmetricService.sm3(noPasswordParam.getPlaintext());
    }

    @PostMapping("asymmetric/rsa/getKeys")
    public R getKeys(@RequestBody @Validated KeysParam keysParam, BindingResult result) {
        if (keysParam.getKeySize() == null) {
            return R.fail("参数异常");
        }
        return symmetricService.getKeys(keysParam.getKeySize());
    }

    @PostMapping("getAllKeys")
    public R getAllKeys(@RequestBody PageParam pageParam) {
        return symmetricService.getAllKeys(pageParam);
    }

    @PostMapping("asymmetric/rsa/encrypt")
    public R rsaEncrypt(@RequestBody @Validated AsymmetricEncryptParam asymmetricEncryptParam, BindingResult result) {
        boolean b = result.hasErrors();
        if (asymmetricEncryptParam.getPlaintext() == null || asymmetricEncryptParam.getPlaintext().equals("") ||
                asymmetricEncryptParam.getPublicKey() == null || asymmetricEncryptParam.getPublicKey().equals("")) {
            return R.fail("参数错误");
        }
        return symmetricService.rsaEncrypt(asymmetricEncryptParam);
    }

    @PostMapping("asymmetric/rsa/decrypt")
    public R rsaDecrypt(@RequestBody AsymmetricDecryptParam asymmetricDecryptParam) {
        if (asymmetricDecryptParam.getCiphertext() == null || asymmetricDecryptParam.getCiphertext().equals("") ||
                asymmetricDecryptParam.getPrivateKey() == null || asymmetricDecryptParam.getPrivateKey().equals("")) {
            return R.fail("参数错误");
        }
        return symmetricService.rsaDecrypt(asymmetricDecryptParam);
    }

    @PostMapping("asymmetric/rsa/getAllEncryption")
    public R rsaGetAllEncryption(@RequestBody PageParam pageParam) {
        return symmetricService.rsaGetAllEncryption(pageParam);
    }

    @PostMapping("asymmetric/rsa/getAllDecryption")
    public R rsaGetAllDecryption(@RequestBody PageParam pageParam) {
        return symmetricService.rsaGetAllDecryption(pageParam);
    }

    @PostMapping("asymmetric/sm2/encrypt")
    public R sm2Encrypt(@RequestBody @Validated AsymmetricEncryptParam asymmetricEncryptParam, BindingResult result) throws Exception {
        boolean b = result.hasErrors();
        if (asymmetricEncryptParam.getPlaintext() == null || asymmetricEncryptParam.getPlaintext().equals("") ||
                asymmetricEncryptParam.getPublicKey() == null || asymmetricEncryptParam.getPublicKey().equals("")) {
            return R.fail("参数错误");
        }
        return symmetricService.sm2Encrypt(asymmetricEncryptParam);
    }

    @PostMapping("asymmetric/sm2/decrypt")
    public R sm2Decrypt(@RequestBody AsymmetricDecryptParam asymmetricDecryptParam) throws Exception {
        if (asymmetricDecryptParam.getCiphertext() == null || asymmetricDecryptParam.getCiphertext().equals("") ||
                asymmetricDecryptParam.getPrivateKey() == null || asymmetricDecryptParam.getPrivateKey().equals("")) {
            return R.fail("参数错误");
        }
        return symmetricService.sm2Decrypt(asymmetricDecryptParam);
    }

}
